package karma.main;

import net.minecraft.client.Minecraft;
import net.minecraft.src.*;

import java.io.*;

public class ItemTeleAmulet extends Item
{
    private static boolean loaded = false;
	public static double x1 = 0;
    public static double x2 = 0;
    public static double x3 = 0;
    public static double x4 = 0;
    public static double x5 = 0;
    public static double x6 = 0;
    public static double x7 = 0;
    public static double x8 = 0;
    public static double x9 = 0;
    public static double y1 = 0;
    public static double y2 = 0;
    public static double y3 = 0;
    public static double y4 = 0;
    public static double y5 = 0;
    public static double y6 = 0;
    public static double y7 = 0;
    public static double y8 = 0;
    public static double y9 = 0;
    public static double z1 = 0;
    public static double z2 = 0;
    public static double z3 = 0;
    public static double z4 = 0;
    public static double z5 = 0;
    public static double z6 = 0;
    public static double z7 = 0;
    public static double z8 = 0;
    public static double z9 = 0;
    public static String n1 = "Location 1";
    public static String n2 = "Location 2";
    public static String n3 = "Location 3";
    public static String n4 = "Location 4";
    public static String n5 = "Location 5";
    public static String n6 = "Location 6";
    public static String n7 = "Location 7";
    public static String n8 = "Location 8";
    public static String n9 = "Location 9";

    public ItemTeleAmulet(int i)
    {
        super(i);
        maxStackSize = 1;
    }

    /**
     * Return an item rarity from EnumRarity
     */
    public EnumRarity getRarity(ItemStack itemstack)
    {
        return mod_KarmaCraft.ENCHANTED;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
    {
        ModLoader.openGUI(entityplayer, new GuiTeleAmulet());
        return super.onItemRightClick(itemstack, world, entityplayer);
    }

    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    
    public static void writeNBTToFile() 
    {
    	try 
    	{
    		File file = new File(ModLoader.getMinecraftInstance().getMinecraftDir() + "/saves/" + ModLoader.getMinecraftInstance().theWorld.getSaveHandler().getSaveDirectoryName() + "","KarmaTeleLocations.dat");
    		if (!file.exists()) 
    		{
    			file.createNewFile();
    		}
    		FileOutputStream fileoutputstream = new FileOutputStream(file);
    		NBTTagCompound nbt = new NBTTagCompound();
    		nbt.setDouble("x1", EnumTeleportLocation.l1.xpos);
            nbt.setDouble("x2", EnumTeleportLocation.l2.xpos);
            nbt.setDouble("x3", EnumTeleportLocation.l3.xpos);
            nbt.setDouble("x4", EnumTeleportLocation.l4.xpos);
            nbt.setDouble("x5", EnumTeleportLocation.l5.xpos);
            nbt.setDouble("x6", EnumTeleportLocation.l6.xpos);
            nbt.setDouble("x7", EnumTeleportLocation.l7.xpos);
            nbt.setDouble("x8", EnumTeleportLocation.l8.xpos);
            nbt.setDouble("x9", EnumTeleportLocation.l9.xpos);
            nbt.setDouble("y1", EnumTeleportLocation.l1.ypos);
            nbt.setDouble("y2", EnumTeleportLocation.l2.ypos);
            nbt.setDouble("y3", EnumTeleportLocation.l3.ypos);
            nbt.setDouble("y4", EnumTeleportLocation.l4.ypos);
            nbt.setDouble("y5", EnumTeleportLocation.l5.ypos);
            nbt.setDouble("y6", EnumTeleportLocation.l6.ypos);
            nbt.setDouble("y7", EnumTeleportLocation.l7.ypos);
            nbt.setDouble("y8", EnumTeleportLocation.l8.ypos);
            nbt.setDouble("y9", EnumTeleportLocation.l9.ypos);
            nbt.setDouble("z1", EnumTeleportLocation.l1.zpos);
            nbt.setDouble("z2", EnumTeleportLocation.l2.zpos);
            nbt.setDouble("z3", EnumTeleportLocation.l3.zpos);
            nbt.setDouble("z4", EnumTeleportLocation.l4.zpos);
            nbt.setDouble("z5", EnumTeleportLocation.l5.zpos);
            nbt.setDouble("z6", EnumTeleportLocation.l6.zpos);
            nbt.setDouble("z7", EnumTeleportLocation.l7.zpos);
            nbt.setDouble("z8", EnumTeleportLocation.l8.zpos);
            nbt.setDouble("z9", EnumTeleportLocation.l9.zpos);
            nbt.setString("n1", EnumTeleportLocation.l1.locname);
            nbt.setString("n2", EnumTeleportLocation.l2.locname);
            nbt.setString("n3", EnumTeleportLocation.l3.locname);
            nbt.setString("n4", EnumTeleportLocation.l4.locname);
            nbt.setString("n5", EnumTeleportLocation.l5.locname);
            nbt.setString("n6", EnumTeleportLocation.l6.locname);
            nbt.setString("n7", EnumTeleportLocation.l7.locname);
            nbt.setString("n8", EnumTeleportLocation.l8.locname);
            nbt.setString("n9", EnumTeleportLocation.l9.locname);
    		CompressedStreamTools.writeCompressed(nbt, fileoutputstream);
    		fileoutputstream.close();
    	}
    	catch(Exception exception) 
    	{
    		exception.printStackTrace();
    	}
    }

    public static void readNBTFromFile() 
    {
    	ModLoader.getMinecraftInstance().theWorld.checkSessionLock();
    	try 
    	{
    		File file = new File ("" + ModLoader.getMinecraftInstance().getMinecraftDir() + "/saves/" + ModLoader.getMinecraftInstance().theWorld.getSaveHandler().getSaveDirectoryName() + "","KarmaTeleLocations.dat");
    		if (!file.exists()) 
    		{
    			return;
    		}
    		FileInputStream fileinputstream = new FileInputStream(file);
    		NBTTagCompound nbt = CompressedStreamTools.readCompressed(fileinputstream);
    		if (nbt.hasKey("x1"))
            {
                x1 = nbt.getDouble("x1");
            }

            if (nbt.hasKey("y1"))
            {
                y1 = nbt.getDouble("y1");
            }

            if (nbt.hasKey("z1"))
            {
                z1 = nbt.getDouble("z1");
            }

            if (nbt.hasKey("n1"))
            {
                n1 = nbt.getString("n1");
            }

            if (nbt.hasKey("x2"))
            {
                x2 = nbt.getDouble("x2");
            }

            if (nbt.hasKey("y2"))
            {
                y2 = nbt.getDouble("y2");
            }

            if (nbt.hasKey("z2"))
            {
                z2 = nbt.getDouble("z2");
            }

            if (nbt.hasKey("n2"))
            {
                n2 = nbt.getString("n2");
            }

            if (nbt.hasKey("x3"))
            {
                x3 = nbt.getDouble("x3");
            }

            if (nbt.hasKey("y3"))
            {
                y3 = nbt.getDouble("y3");
            }

            if (nbt.hasKey("z3"))
            {
                z3 = nbt.getDouble("z3");
            }

            if (nbt.hasKey("n3"))
            {
                n3 = nbt.getString("n3");
            }

            if (nbt.hasKey("x4"))
            {
                x4 = nbt.getDouble("x4");
            }

            if (nbt.hasKey("y4"))
            {
                y4 = nbt.getDouble("y4");
            }

            if (nbt.hasKey("z4"))
            {
                z4 = nbt.getDouble("z4");
            }

            if (nbt.hasKey("n4"))
            {
                n4 = nbt.getString("n4");
            }

            if (nbt.hasKey("x5"))
            {
                x5 = nbt.getDouble("x5");
            }

            if (nbt.hasKey("y5"))
            {
                y5 = nbt.getDouble("y5");
            }

            if (nbt.hasKey("z5"))
            {
                z5 = nbt.getDouble("z5");
            }

            if (nbt.hasKey("n5"))
            {
                n5 = nbt.getString("n5");
            }

            if (nbt.hasKey("x6"))
            {
                x6 = nbt.getDouble("x6");
            }

            if (nbt.hasKey("y6"))
            {
                y6 = nbt.getDouble("y6");
            }

            if (nbt.hasKey("z6"))
            {
                z6 = nbt.getDouble("z6");
            }

            if (nbt.hasKey("n6"))
            {
                n6 = nbt.getString("n6");
            }

            if (nbt.hasKey("x7"))
            {
                x7 = nbt.getDouble("x7");
            }

            if (nbt.hasKey("y7"))
            {
                y7 = nbt.getDouble("y7");
            }

            if (nbt.hasKey("z7"))
            {
                z7 = nbt.getDouble("z7");
            }

            if (nbt.hasKey("n7"))
            {
                n7 = nbt.getString("n7");
            }

            if (nbt.hasKey("x8"))
            {
                x8 = nbt.getDouble("x8");
            }

            if (nbt.hasKey("y8"))
            {
                y8 = nbt.getDouble("y8");
            }

            if (nbt.hasKey("z8"))
            {
                z8 = nbt.getDouble("z8");
            }

            if (nbt.hasKey("n8"))
            {
                n8 = nbt.getString("n8");
            }

            if (nbt.hasKey("x9"))
            {
                x9 = nbt.getDouble("x9");
            }

            if (nbt.hasKey("y9"))
            {
                y9 = nbt.getDouble("y9");
            }

            if (nbt.hasKey("z9"))
            {
                z9 = nbt.getDouble("z9");
            }

            if (nbt.hasKey("n9"))
            {
                n9 = nbt.getString("n9");
            }	
    		fileinputstream.close();
    	}
    	catch (Exception exception) 
    	{
    		exception.printStackTrace();
    	}
    }
    
    public static void doStuff()
    {
    	if(GuiTeleAmulet.save)
    	{
    		writeNBTToFile();
    	}
    	else
    	{
    		readNBTFromFile();
    	}
    	
    	if (GuiTeleAmulet.channeltimer < 25 && GuiTeleAmulet.channeling)
    	{
    		GuiTeleAmulet.channeltimer ++;
    		GuiTeleAmulet.channelTP();
    		GuiKarmaHUD guikarmahud = new GuiKarmaHUD();
    		guikarmahud.render("channeltp");
    	}
    }

	public static boolean allowTP(EntityPlayer player, double d, double d1, double d2) 
	{
		int totdis = getTotalDistance(player, d, d1, d2);
		if (totdis <= 100) //if dis not > 100
		{
			return true;
		}
		else //if totdis > 100
		{
			int cost = (int)totdis / 100;
			int stack = mod_KarmaCraft.stackInInventory(mod_KarmaCraft.MagicPowder, player);
			if (player.inventory.getStackInSlot(stack).stackSize >= cost) //if they have magic powder && dis > 100
			{
				int times = cost;
				while (times > 0)
				{
					player.inventory.consumeInventoryItem(mod_KarmaCraft.MagicPowder.shiftedIndex);
					times --;
				}
				return true;
			}
			else //if they dont have magic powder && distance > 100
			{
				return false;
			}
		}
	}
	
	public static int getTotalDistance(EntityPlayer player, double d, double d1, double d2)
	{
		double posX = player.posX;
		double posY = player.posY;
		double posZ = player.posZ;
		double xdis = Math.abs(d - posX);
		double ydis = Math.abs(d1 - posY);
		double zdis = Math.abs(d2 - posZ);
		int xzdis = (int) Math.round(Math.abs(Math.sqrt((xdis * xdis) + (zdis * zdis))));
		int totdis = (int) Math.round(Math.abs(Math.sqrt((xzdis * xzdis) + (ydis * ydis))));
		return totdis;
	}
}
